package com;
import java.awt.*;
import java.awt.event.*;

import static java.lang.System.exit;

public class TankFrame extends Frame {
    int x = 200, y = 200;

    boolean BL=false;
    boolean BR=false;
    boolean BU=false;
    boolean BD=false;
    public TankFrame() {
        setSize(600, 600);
        setResizable(false);
        setTitle("Tank War");
        setVisible(true);
        this.addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {});
    }

    @Override
    public void paint(Graphics g) {
        //System.out.println("paint");
        g.fillRect(x, y, 50, 50);
        //x += 10;
    }

    class MyKeyListener extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            //按下去
            //System.out.println("pressed");
            int key=e.getKeyCode();
            switch (key){
                    case KeyEvent.VK_LEFT:
                       BL=true;
                    break;
                    case KeyEvent.VK_RIGHT:
                        BR=true;
                    break;
                    case KeyEvent.VK_UP:
                        BU=true;
                    break;
                    case KeyEvent.VK_DOWN:
                        BD=true;
                    break;
                default:
                    break;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            //抬起来
            //System.out.println("released");
            int key=e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    BL=false;
                    break;
                case KeyEvent.VK_RIGHT:
                    BR=false;
                    break;
                case KeyEvent.VK_UP:
                    BU=false;
                    break;
                case KeyEvent.VK_DOWN:
                    BD=false;
                    break;
                default:
                    break;
            }
        }
    }
}
